package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "后台控制营业状态")
public class shopController {


    @Autowired
    private RedisTemplate<String, Integer> redisTemplate;

    private final String SHOP_STATUS_KEY="SHOP_STATUS";

    /*
    * 设置营业状态
    * */
    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result<String> setStatus(@PathVariable Integer status){
        // 1.设置营业状态
        log.info("设置营业状态为：{}",status==1?"营业中":"打烊了");
        redisTemplate.opsForValue().set(SHOP_STATUS_KEY,status);
        String i=status==1?"营业中":"打烊了";
        return Result.success("当前营业状态为:"+i);
    }

    /*
     * 获取营业状态
     * */
    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result<Integer> getStatus(){
        // 1.获取营业状态
        Integer status = redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        String i=(status==1 ? "营业中":"打烊了");
        log.info("获取营业成功！当前营业状态为：{}",i);
        return Result.success(status); // 返回营业状态值
    }


}
